//
//  YTTDataManager.m
//  跑圈儿
//
//  Created by dllo on 16/4/25.
//  Copyright © 2016年 唐康. All rights reserved.
//

#import "YTTDataManager.h"

@implementation YTTDataManager

+(YTTDataManager *)shareInstance{
    static YTTDataManager *dbManager = nil;
    if (!dbManager) {
        dbManager = [[YTTDataManager alloc] init];
    }
    return  dbManager;
}



-(void)openDB{
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSLog(@"%@",path);
    
    self.filePath = [path stringByAppendingPathComponent:@"RunTM.db"];
    self.db = [[FMDatabase alloc] initWithPath:_filePath];
    BOOL result = [_db open];
    if (result) {
        NSLog(@"打开成功");
    }  else{
        NSLog(@"打开数据库失败");
    }
    
}


-(void)closeDB{
    [_db close];
}

-(void)creatTable{
    
    NSString *creatSQL = @"CREATE TABLE IF NOT EXISTS hupurunTM (num INTEGER PRIMARY KEY AUTOINCREMENT, miter TEXT, time TEXT,calendar TEXT ,timeStamp TEXT)";
    [self executeSQL:creatSQL message:@"创建表"];
    
}

-(void)dropTalbe{
    
    NSString *dropSQL = @"DROP TABLE hupurunTM";
    [self executeSQL:dropSQL message:@"删除表"];
    
}

-(BOOL)insertMiter:(CGFloat)miter Time:(NSString *)time Calendar:(NSString *)calendar TimeStamp:(NSString *)timeStamp {
    
    NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO hupurunTM (miter, time, calendar,timeStamp) VALUES('%f' ,'%@','%@','%@')",miter,time,calendar,timeStamp];
    
    BOOL r = [_db executeUpdate:insertSQL];
    if (r) {
        NSLog(@"INSERT成功");
        
    } else {
        
        NSLog(@"INSERT失败");
    }
    return r;
    
}

- (void) delegateData {
    
    //时间转换时间戳(获取当前时间转换成时间戳)
    self.datenow = [NSDate date];
    NSInteger m = 300;
    self.timeStamp = [NSString stringWithFormat:@"%ld",(long)([self.datenow timeIntervalSince1970] - m)];
    NSLog(@"TM_时间戳 %@",self.timeStamp);
    //    NSInteger m  = 604800;
    NSLog(@"78887  %@",[NSString stringWithFormat:@"%ld",(long)([self.datenow timeIntervalSince1970] - m)]);
    
    
    
    //    NSTimeInterval timeInterval = [@"%@",[NSString stringWithFormat:@"%ld",(long)([self.datenow timeIntervalSince1970] + m)]  doubleValue]; //减去七天的秒数 转换成NSTimeInterval
    //    //时间戳转换成时间
    //    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    //    NSString *str = [NSString stringWithFormat:@"%@",date2];
    //    NSLog(@"_ss__ %@", str);
    //    //截取转换后的字符串
    //    NSString *newStr = [str substringWithRange:NSMakeRange(0, 20)];
    //    NSLog(@"lsls__ %@",newStr);
    //    NSLog(@" date2 -------------------- %@",date2);
    
    //删除日期早于七日之前的数据
    NSString *deleteSQL = [NSString stringWithFormat:@"delete from hupurunTM where timeStamp < '%@'",self.timeStamp];
    [self executeSQL:deleteSQL message:@"删除"];
}

-(void)executeSQL:(NSString *)sql message:(NSString *)mes{
    BOOL result = [_db executeUpdate:sql];
    if (result) {
        NSLog(@"%@成功",mes);
    }else{
        NSLog(@"%@失败",mes);
    }
}


-(NSMutableArray *)selectAll{
    NSMutableArray *mArr = [NSMutableArray array];
    NSString *selectSQL = @"SELECT *FROM hupurunTM";
    FMResultSet *set = [_db executeQuery:selectSQL];
    while ([set next]) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setObject:[set stringForColumn:@"miter"] forKey:@"miter"];
        [dic setObject:[set stringForColumn:@"time"] forKey:@"time"];
        [dic setObject:[set stringForColumn:@"calendar"] forKey:@"calendar"];
        
        [mArr addObject:dic];
    }
    
    return mArr;
}











@end
